﻿namespace nBayes
{
  using System;
  using System.IO;
  using System.Xml.Serialization;

  public class FileIndex : Index, FileIndexInterface
  {
    private MemoryIndex index = new MemoryIndex();
    private string filePath;

    // private static object synclock = new object();

    public FileIndex(string filePath)
    {
      if (string.IsNullOrEmpty(filePath))
      {
        throw new ArgumentNullException("filePath");
      }

      this.filePath = filePath;
    }

    public override int EntryCount
    {
      get { return this.index.EntryCount; }
    }

    /// <exception cref="InvalidOperationException">Occurs when the serializer has trouble
    /// deserializing the file on disk. Can occur if the file is corrupted.</exception>
    public void Open()
    {
      if (File.Exists(this.filePath))
      {
        //lock (synclock)
        using (Stream stream = File.OpenRead(this.filePath))
        {
          XmlSerializer serializer = new XmlSerializer(typeof(IndexTable<string, int>));
          index.table = serializer.Deserialize(stream) as IndexTable<string, int>;
        }
      }
    }
    
    public void AddAndSave(Entry document)
    {
        this.Add(document);
        
        this.Save();
    }

    public override void Add(Entry document)
    {
      this.index.Add(document);
    }

    public void Save()
    {
      // lock (synclock)
      using (Stream stream = File.Open(this.filePath, FileMode.OpenOrCreate, FileAccess.Write))
      {
        XmlSerializer serializer = new XmlSerializer(typeof(IndexTable<string, int>));
        serializer.Serialize(stream, index.table);
        stream.Close();
      }
    }

    public override int GetTokenCount(string token)
    {
      return this.index.GetTokenCount(token);
    }
  }
}
